<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    :close-on-click-modal="false"
    @close="handleCloseDialog"
    @open="handleOpenDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-loading="loading" v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="clickAssignRoles"
        >确定</el-button
      >
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from "@/api/setting";
import { reqGetBaseInfo } from "@/api/user";
import { reqAssignRoles } from "@/api/employees";
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      loading: false,
    };
  },
  methods: {
    async clickAssignRoles() {
      const res = await reqAssignRoles(this.userId, this.checkList);
      console.log(res);
      this.$message.success("分配角色成功");
      this.handleCloseDialog();
    },
    handleOpenDialog() {
      this.loading = true;
      //   this.getRoleList();
      //   this.getEmployeeDetail();
      // 需求:需要在两个数据都回来之后再关掉loading
      Promise.all([this.getRoleList(), this.getEmployeeDetail()]).then(() => {
        this.loading = false;
      });
    },
    async getEmployeeDetail() {
      const {
        data: { roleIds },
      } = await reqGetBaseInfo(this.userId);
      this.checkList = roleIds;
    },
    async getRoleList() {
      const {
        data: { rows },
      } = await reqGetRoleList(1, 999);
      this.roleList = rows;
    },
    handleCloseDialog() {
      this.$emit("update:showRoleDialog", false);
      this.checkList = [];
    },
  },
};
</script>

<style>
</style>